package depot;

import java.util.HashMap;

public class ParcelMap extends Exception{
	
	private HashMap<String, Parcel> parcelMap;
	
	/**
	 * Constructor to create an object with HashMap of Parcels
	 */
	public ParcelMap(){
		parcelMap = new HashMap<String, Parcel>();
	}

	/**
	 * Method for accessing parcel hash map
	 * @return parcelMap
	 */
	public HashMap<String, Parcel> getParcelMap(){
		return parcelMap;
	}
	
	public int getSize()
	{
		return parcelMap.size();
	}
	/**
	 * This method is used to put a new parcel into the parcelMap
	 * @param p
	 * @throws DuplicateIDException
	 */
	public void putParcel(Parcel p) throws DuplicateIDException{
		
		String ID = p.getParcelID();
		if(parcelMap.containsKey(ID))
			throw new DuplicateIDException(ID);
		else 
			parcelMap.put(ID, p);
	}
	
	
	/**
	 * This method is used to find a parcel from the parcelMap using its parcelID
	 * @param ID
	 * @return Parcel
	 * @throws NoExistParcelException
	 */
	public Parcel findParcel(String ID) throws NoExistParcelException 
    {
    	if(parcelMap.containsKey(ID))
    		return parcelMap.get(ID);
    	else
    		throw new NoExistParcelException(ID);
    }
	
	
	/**
	 * This method is used to remove a parcel if it is in the parcelMap
	 * @param ID
	 * @return true or false
	 */
	public boolean removeParcel(String ID){
		if(parcelMap.containsKey(ID)){
    		parcelMap.remove(ID);
    		return true;
    	}
		return false;
	}
	
	/**
	 * This method is used to process the data in line
	 * @param line
	 * @throws IDFormatException
	 * @throws DimensionFormatException
	 */
	public void processLine(String line) throws IDFormatException, DimensionFormatException{
		try{
			String parcelID = "";
			int day = 0;
			double weight = 0;
			String dimension = "";
			String parts [] = line.split(",");
			if (parts.length != 4){
				System.out.println("The length of the list is not correct.");
			}
			else{
				parcelID = parts[0];
				
			    day = Integer.parseInt(parts[1]);
			    
			    weight = Double.parseDouble(parts[2]);
			    
			    dimension = parts[3];
			}
			
			Parcel p;
				p = new Parcel(parcelID, day, weight, dimension);
			this.putParcel(p);
			
		}catch(NumberFormatException nfe){
			System.out.println(nfe.getMessage());
		}catch(DuplicateIDException die){
			System.out.println(die.getMessage());
		}
	}
}
